//
//  XDLMGoodPlanViewController.m
//  xindailianmeng
//
//  Created by xu on 17/3/18.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDLMGoodPlanViewController.h"
#import "XDLMInvestHistoryViewController.h"


@interface XDLMGoodPlanViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong)UIScrollView           *bgScroll;
@property (nonatomic, strong)UILabel                *lixiLabel;
@property (nonatomic, strong)UILabel                *shouyiLabel;
@property (nonatomic, strong)UIPickerView            * pickerView;//自定义pickerview
@property (nonatomic, strong)NSMutableArray          * monkyArr;//保存要展示的字母
@property (nonatomic, strong)NSArray                 * number;//
@property (nonatomic, strong)UITextField                  *showView;
@property (nonatomic, strong)UIImageView             *cenview;
@property (nonatomic, assign)BOOL                    selectType;
@property (nonatomic, strong)NSArray                *emptyArr;
@property (nonatomic, strong)UIButton               *qiehuanbtn;
@property (nonatomic, assign)CGFloat                midlleRate;
@property (nonatomic, assign)CGFloat                moneyNum;
@property (nonatomic, assign)NSInteger              middleMonth;

@end

@implementation XDLMGoodPlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self buildLayout];

    // Do any additional setup after loading the view.
}



- (void)buildLayout{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged) name:UITextFieldTextDidChangeNotification object:self.showView];
#warning 请求完数据设置数组第一个元素
    self.midlleRate = 0.03;
    self.middleMonth = 3;
    self.moneyNum = 100;
    self.selectType = YES;
    self.emptyArr = [[NSArray alloc]init];
    self.view.backgroundColor = [UIColor whiteColor];
    self.bgScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    self.bgScroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bgScroll];
    self.title = @"优选计划";
    UIImage *im = [UIImage imageNamed:@"head_im"];
    UIImageView *headview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, kScreenW, im.size.height)];
    headview.image = im;
    [self.bgScroll addSubview:headview];
    
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(kScreenW/2, headview.frame.origin.y + headview.frame.size.height + 15, 1/[UIScreen mainScreen].scale, 92/2)];
    line.backgroundColor = [LCCommon getColor:@"dedede"];
    [self.bgScroll addSubview:line];
    
    
    UIImage *center =  [UIImage imageNamed:@"center_im"];
    self.cenview = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW/2 - center.size.width/2, line.frame.origin.y + line.frame.size.height + 15, center.size.width, center.size.height)];
    self.cenview.image = center;
    self.cenview.userInteractionEnabled = YES;
    [self.bgScroll addSubview:self.cenview];
    
    
    self.lixiLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.cenview.frame.origin.x - 5, line.frame.origin.y + 2, kScreenW/2-(self.cenview.frame.origin.x), 19)];
    self.lixiLabel.font = [UIFont systemFontOfSize:19];
    self.lixiLabel.textColor = [LCCommon getColor:@"333333"];
#warning 请求完数据设置数组第一个元素
    self.lixiLabel.text = @"3%";
    self.lixiLabel.textAlignment = NSTextAlignmentCenter;
    [self.bgScroll addSubview:self.lixiLabel];
    
    
    self.shouyiLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/2  + 5, line.frame.origin.y + 2, kScreenW/2-(self.cenview.frame.origin.x), 19)];
    self.shouyiLabel.font = [UIFont systemFontOfSize:19];
    self.shouyiLabel.textColor = [LCCommon getColor:@"333333"];
    self.shouyiLabel.text = [self makeInterestWithPrincipal:self.moneyNum Month:self.middleMonth andInterestRate:self.midlleRate];
    self.shouyiLabel.textAlignment = NSTextAlignmentCenter;
    [self.bgScroll addSubview:self.shouyiLabel];
    
    UILabel *lixi = [[UILabel alloc]initWithFrame:CGRectMake(self.cenview.frame.origin.x - 5, self.lixiLabel.frame.origin.y + 19 + 10, kScreenW/2-(self.cenview.frame.origin.x), 11)];
    lixi.font = [UIFont systemFontOfSize:11];
    lixi.textColor = [LCCommon getColor:@"999999"];
    lixi.text = @"年华利率";
    lixi.textAlignment = NSTextAlignmentCenter;
    [self.bgScroll addSubview:lixi];

    
    UILabel *shouyi = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/2 + 5, self.lixiLabel.frame.origin.y + 19 + 10, kScreenW/2-(self.cenview.frame.origin.x), 11)];
    shouyi.font = [UIFont systemFontOfSize:11];
    shouyi.textColor = [LCCommon getColor:@"999999"];
    shouyi.text = @"预计收益";
    shouyi.textAlignment = NSTextAlignmentCenter;
    [self.bgScroll addSubview:shouyi];
    
    
    UIImage *btnim = [UIImage imageNamed:@"now_join"];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenW/2 - btnim.size.width/2, self.cenview.frame.origin.y + self.cenview.frame.size.height + 40/2, btnim.size.width, btnim.size.height)];
    [btn setImage:btnim forState:UIControlStateNormal];
    [self.bgScroll addSubview:btn];
    
    self.monkyArr = [[NSMutableArray alloc]init];
    NSInteger  num = 100;
    
    do {
        [self.monkyArr addObject:[NSNumber numberWithInteger:num]];
        num +=100;
    } while (num >0 && num < 1000);
    
    do {
        [self.monkyArr addObject:[NSNumber numberWithInteger:num]];
        num +=1000;
    } while (num >1000 && num < 201000);
    
    self.number = [NSArray arrayWithObjects:@"3",@"6",@"9",@"12", nil];
    // 初始化pickerView
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 32, self.cenview.frame.size.width, self.cenview.frame.size.height - 42)];
    [self.cenview addSubview:self.pickerView];
    
    //指定数据源和委托
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
    
    UILabel *yuan = [[UILabel alloc]initWithFrame:CGRectMake(self.cenview.frame.size.width/3, self.cenview.frame.size.height/2 + 11/2, 11, 11)];
    yuan.font = [UIFont systemFontOfSize:11];
    yuan.textColor = [LCCommon getColor:@"666666"];
    yuan.text = @"元";
    [self.cenview addSubview:yuan];
    
    
    UILabel *yue = [[UILabel alloc]initWithFrame:CGRectMake(3 * self.cenview.frame.size.width / 4 + 18, yuan.frame.origin.y, 25, 11)];
    yue.font = [UIFont systemFontOfSize:11];
    yue.textColor = [LCCommon getColor:@"666666"];
    yue.text = @"个月";
    [self.cenview addSubview:yue];
    
    
    UILabel *jine = [[UILabel alloc]initWithFrame:CGRectMake(96/2, 15, 0, 14)];
    jine.font = [UIFont systemFontOfSize:14];
    jine.textColor = [LCCommon getColor:@"666666"];
    jine.text = @"投资金额";
    [jine sizeToFit];
    [self.cenview addSubview:jine];
    
    
    UILabel *qixian = [[UILabel alloc]initWithFrame:CGRectMake(96/2, 15, 0, 14)];
    qixian.font = [UIFont systemFontOfSize:14];
    qixian.textColor = [LCCommon getColor:@"666666"];
    qixian.text = @"封闭期限";
    [qixian sizeToFit];
    [self.cenview addSubview:qixian];
    qixian.frame = CGRectMake(self.cenview.frame.size.width - 96/2 - qixian.frame.size.width, 15, qixian.frame.size.width, 14);
    
    UIImage *qiehuan = [UIImage imageNamed:@"qihuan_btn"];
    UIImageView *qiehuanview = [[UIImageView alloc]initWithFrame:CGRectMake(jine.frame.size.width + jine.frame.origin.x + 5, jine.frame.origin.y + jine.frame.size.height/2 - qiehuan.size.height/2, qiehuan.size.width, qiehuan.size.height)];
    qiehuanview.image = qiehuan;
    [self.cenview addSubview:qiehuanview];
    
    
    self.qiehuanbtn = [[UIButton alloc]initWithFrame:CGRectMake(qiehuanview.frame.origin.x, 0, 15 + 14 + 15, 15 + 14 + 15)];
    [self.qiehuanbtn addTarget:self action:@selector(clickToqihuan:) forControlEvents:UIControlEventTouchUpInside];
    self.qiehuanbtn.tag = 1024;
    [self.cenview addSubview:self.qiehuanbtn];
    
    self.showView = [[UITextField alloc]initWithFrame:CGRectMake(82/2, 108/2 + 26/2 + 42 + 2, self.cenview.frame.size.width/2 - 82/2,13)];
    //self.showView.textColor = [LCCommon getColor:@"666666"];
    self.showView.placeholder = @"请输入投资金额";
    self.showView.font = [UIFont systemFontOfSize:13];
    self.showView.backgroundColor = [UIColor whiteColor];
    self.showView.keyboardType = UIKeyboardTypeNumberPad;
}

- (void)clickToqihuan:(UIButton *)sender{
    //[self.cenview addSubview:self.showView];
    if (sender.tag == 1024) {
        self.selectType = NO;
        [self.pickerView reloadComponent:0];
        [self.cenview addSubview:self.showView];
        self.showView.text = @"";
        self.moneyNum = 0;
        self.shouyiLabel.text = @"0元";
        self.qiehuanbtn.tag = 1025;
    }else{
        self.selectType = YES;
        self.moneyNum = 100;
        self.shouyiLabel.text = [self makeInterestWithPrincipal:self.moneyNum Month:self.middleMonth andInterestRate:self.midlleRate];
        [self.showView removeFromSuperview];
        [self.pickerView reloadComponent:0];
        self.qiehuanbtn.tag = 1024;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setOpaque:NO];
    [self.navigationController.navigationBar setTranslucent:NO];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    [self.navigationController.navigationBar setOpaque:NO];
    [self.navigationController.navigationBar setTranslucent:NO];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
}


//指定pickerview有几个表盘
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;//第一个展示字母、第二个展示数字
}

//指定每个表盘上有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger result = 0;
    switch (component) {
        case 0:
            if (self.selectType) {
                result = self.monkyArr.count;//根据数组的元素个数返回几行数据
            }else{
                result = self.emptyArr.count;
            }
            
            break;
        case 1:
            
            break;
        case 2:
            result = self.number.count;
            break;
        default:
            break;
    }
    
    return result;
}

#pragma mark UIPickerView Delegate Method 代理方法


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 1) {
        return (self.pickerView.frame.size.width/3 - 50);
    }
    return self.pickerView.frame.size.width/3;
}


//指定每行如何展示数据（此处和tableview类似）
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString * title = nil;
    switch (component) {
        case 0:
            title = [NSString stringWithFormat:@"%@",self.monkyArr[row]];
            break;
        case 1:
            break;
        case 2:
            title = [NSString stringWithFormat:@"%@",self.number[row]];
            break;
        default:
            break;
    }
    
    return title;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 39;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    CGFloat width =  120;
    CGFloat height = 60.0f;
    
    UIView * myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    myView.backgroundColor = [UIColor clearColor];
    
    UILabel * complateLabel = [[UILabel alloc] init];
    complateLabel.center = myView.center;
    complateLabel.bounds = CGRectMake(0, 0, width, height);
    //complateLabel.textColor = YSColor(88, 164, 240);
    complateLabel.textAlignment = NSTextAlignmentCenter;
    //complateLabel.font = FONTWITHSIZE_LIGHT(14);
    if (component == 0) {
        complateLabel.text =  [NSString stringWithFormat:@"%@",self.monkyArr[row]];
    }else if(component == 2){
        complateLabel.text =  [NSString stringWithFormat:@"%@",self.number[row]];
    }

    [myView addSubview:complateLabel];
    
    ((UIView *)[self.pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
    ((UIView *)[self.pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];

    return myView;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(0 == component){
        self.moneyNum = [self.monkyArr[row] floatValue];
        self.shouyiLabel.text = [self makeInterestWithPrincipal:self.moneyNum Month:self.middleMonth andInterestRate:self.midlleRate];
    }else if (component == 2){
        NSInteger   num = [self.number[row] integerValue];
        switch (num) {
#warning 修改利息
            case 3:
                self.lixiLabel.text = @"3%";
                self.midlleRate = 0.003;
                break;
            case 6:
                self.lixiLabel.text = @"6%";
                self.midlleRate = 0.006;
                break;
            case 9:
                self.lixiLabel.text = @"9%";
                self.midlleRate = 0.009;
                break;
            case 12:
                self.lixiLabel.text = @"12%";
                self.midlleRate = 0.012;
                break;
                
            default:
                break;
        }
        self.middleMonth = num;
        self.shouyiLabel.text = [self makeInterestWithPrincipal:self.moneyNum Month:self.middleMonth andInterestRate:self.midlleRate];
    }
}

- (void)btnpress{
    XDLMInvestHistoryViewController *his = [[XDLMInvestHistoryViewController alloc]init];
    [self.navigationController pushViewController:his animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSString *)makeInterestWithPrincipal:(CGFloat)principal  Month:(NSInteger)month andInterestRate:(CGFloat)rate{
    NSString *interest;
    
    NSDate * mydate = [NSDate date];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = nil;
    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:mydate];
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:month];
    [adcomps setDay:0];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:mydate options:0];
    long long time = newdate.timeIntervalSince1970 - mydate.timeIntervalSince1970;
    int betweenDay = (int)(time / (60*60*24));
    
    CGFloat returnNum = principal + principal * rate / 365 * betweenDay;
    
    interest = [NSString stringWithFormat:@"%.2f元",returnNum];
    
    return interest;
}

- (void)textFieldChanged{
    CGFloat monky = [self.showView.text floatValue];
    self.moneyNum = monky;
    self.shouyiLabel.text = [self makeInterestWithPrincipal:self.moneyNum Month:self.middleMonth andInterestRate:self.midlleRate];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
